package cn.stylefeng.guns.modular.business.frequency.syssitecity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.syssitecity.entity.SiteCity;
import cn.stylefeng.guns.modular.business.frequency.syssitecity.enums.SysSiteCityExceptionEnum;
import cn.stylefeng.guns.modular.business.frequency.syssitecity.mapper.SiteCityMapper;
import cn.stylefeng.guns.modular.business.frequency.syssitecity.model.param.SysSiteCityParam;
import cn.stylefeng.guns.modular.business.frequency.syssitecity.service.SiteCityService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * sys_site_city 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:17
 */
@Service
public class SiteCityServiceImpl extends ServiceImpl<SiteCityMapper, SiteCity> implements SiteCityService {

    @Override
    public PageResult<SiteCity> page(SysSiteCityParam sysSiteCityParam) {

        // 构造条件
        LambdaQueryWrapper<SiteCity> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<SiteCity> list(SysSiteCityParam sysSiteCityParam) {

        // 构造条件
        LambdaQueryWrapper<SiteCity> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(SysSiteCityParam sysSiteCityParam) {

        // 将dto转为实体
        SiteCity sysSiteCity = new SiteCity();
        BeanUtil.copyProperties(sysSiteCityParam, sysSiteCity);

        this.save(sysSiteCity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysSiteCityParam sysSiteCityParam) {
        this.removeById(sysSiteCityParam.getId());
    }

    @Override
    public void edit(SysSiteCityParam sysSiteCityParam) {

        // 根据id查询实体
        SiteCity sysSiteCity = this.querySysSiteCity(sysSiteCityParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(sysSiteCityParam, sysSiteCity);

        this.updateById(sysSiteCity);
    }

    @Override
    public SiteCity detail(SysSiteCityParam sysSiteCityParam) {
        return this.querySysSiteCity(sysSiteCityParam);
    }



    /**
     * 获取sys_site_city
     *
     * @author yujie
     * @since 2023/05/09 17:17
     */
    private SiteCity querySysSiteCity(SysSiteCityParam sysSiteCityParam) {
        SiteCity siteCity = this.getById(sysSiteCityParam.getId());
        if (ObjectUtil.isEmpty(siteCity)) {
            throw new ServiceException(SysSiteCityExceptionEnum.NOT_EXISTED);
        }
        return siteCity;
    }
}